package org.mcjug.model;

import org.mcjug.BaseActivity;
import org.mcjug.R;
import org.mcjug.TimelineActivity;
import org.mcjug.YambaApplicationInterface;

import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;

public class UpdateIntentService extends IntentService {

	private static final String TAG = UpdateIntentService.class.getSimpleName();
	public static final String BROADCAST_INTENT_NAME = "org.mcjug.timelinebroadcast";

	private NotificationManager notificationManager;
	private Notification notification;
	private boolean notify;

	public UpdateIntentService() {
		super(TAG);
		Log.d(TAG, "UpdaterIntentService constructed");
	}

	@Override
	protected void onHandleIntent(Intent arg0) {
		notify = true;
		Log.d(TAG, "in onHandleIntent");
		notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		notification = new Notification(android.R.drawable.stat_notify_chat,
				"", 0);
		YambaApplicationInterface yamba = (YambaApplicationInterface) getApplication();
		int newUpdates = yamba.fetchStatusUpdates();
		if (newUpdates > 0) {
			Log.d(TAG, "We have a new status");
			Intent i = new Intent();
			i.setAction(BROADCAST_INTENT_NAME);
			sendBroadcast(
					i,
					getString(R.string.receive_timeline_notifications_permission_name));

			checkNeedToNotify();
			if (notify) {
				sendTimelineNotification();
			}
		}
		Log.d(TAG, "Done with onHandleIntent");
	}

	private void sendTimelineNotification() {
		Log.d(TAG, "sendTimelineNotification'ing");
		PendingIntent pendingIntent = PendingIntent.getActivity(this, -1,
				new Intent(this, TimelineActivity.class),
				PendingIntent.FLAG_UPDATE_CURRENT);
		notification.when = System.currentTimeMillis();
		notification.flags |= Notification.FLAG_AUTO_CANCEL;
		String notificationTitle = getString(R.string.notification_new_tweet_title);
		String notificationSummary = getString(R.string.notification_new_tweet_summary);
		notification.setLatestEventInfo(getApplicationContext(),
				notificationTitle, notificationSummary, pendingIntent);
		notificationManager.notify(0, notification);
		Log.d(TAG, "sendTimelineNotificationed");
	}

	private void checkNeedToNotify() {
		Log.d(TAG, "About to register donotnotifyreciever");
		registerReceiver(
				doNotNotifyReciever,
				new IntentFilter(BaseActivity.DO_NOT_NOTIFY_INTENT_ACTION_NAME),
				getString(R.string.send_timeline_notifications_permission_name),
				null);

		Log.d(TAG, "about to broadcast should I notify");
		Intent outgoingIntent = new Intent(
				BaseActivity.SHOUL_I_NOTIFY_INTENT_ACTION_NAME);
		sendBroadcast(
				outgoingIntent,
				getString(R.string.receive_timeline_notifications_permission_name));

		Log.d(TAG, "About to sleep for 2 second");
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			Log.e(TAG, "Waiting for do not notify interrupted " + e);
		}

		Log.d(TAG, "About to unregister donotnotifyreciever");
		unregisterReceiver(doNotNotifyReciever);
	}

	BroadcastReceiver doNotNotifyReciever = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			Log.d(TAG,
					"doNotNotifyReciever recieved intent: "
							+ intent.getAction());
			if (intent.getAction().equals(
					BaseActivity.DO_NOT_NOTIFY_INTENT_ACTION_NAME)) {
				Log.d(TAG, "doNotNotifyReciever set notify to false");
				notify = false;
			}
		}
	};
}
